﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace OuSuisJeRest
{
    public enum PushType
    {
        Toast = 1,
        Tile = 2
    }

    public static class PushProvider
    {
        private static string _subscriptionUri = string.Empty;

        /// <summary>
        /// Subscription Uri du channel WP7
        /// </summary>
        /// <param name="uri"></param>
        public static void SetSubscriptionUri(string uri)
        {
            _subscriptionUri = uri;
        }

        /// <summary>
        /// Envoie une notification Push sur WP7
        /// </summary>
        /// <param name="txtMessage"></param>
        /// <param name="subMessage"></param>
        /// <param name="image"></param>
        /// <param name="count"></param>
        /// <param name="pushType"></param>
        public static void SendNotifications(string txtMessage, string subMessage, string image, int count, PushType pushType)
        {
            HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(_subscriptionUri); 

            sendNotificationRequest.Method =  "POST"; 
            //Indicate that you'll send toast notifications!
            sendNotificationRequest.ContentType = "text/xml";
            sendNotificationRequest.Headers = new WebHeaderCollection();
            sendNotificationRequest.Headers.Add("X-NotificationClass", "2");

            if (string.IsNullOrEmpty(txtMessage)) return;

            //Create xml envelope 
            string data = string.Empty;
            switch (pushType)
            {
                case PushType.Toast:
                    data = GetToastXml(txtMessage, subMessage);
                    break;
                case PushType.Tile:
                    data = GetTileXml(image, count, txtMessage);
                    break;
                default:
                    break;
            }

            //Wrap custom data into envelope
            string message = string.Format(data, txtMessage);
            byte[] notificationMessage = Encoding.Default.GetBytes(message);
        
            // Set Content Length 
            sendNotificationRequest.ContentLength = notificationMessage.Length; 
            //Push data to stream 
            using (Stream requestStream = sendNotificationRequest.GetRequestStream()) 
            { 
                requestStream.Write(notificationMessage, 0, notificationMessage.Length);
            } 
    
            //Get reponse for message sending 
            HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
            string notificationStatus = response.Headers["X-NotificationStatus"]; 
            string notificationChannelStatus = response.Headers["X-SubscriptionStatus"]; 
            string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
        }

        /// <summary>
        /// XML Toast
        /// </summary>
        /// <param name="txtMessage"></param>
        /// <param name="subMessage"></param>
        /// <returns></returns>
        private static string GetToastXml(string txtMessage, string subMessage)
        {
            return string.Format("X-WindowsPhone-Target: toast\r\n\r\n" +
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<wp:Notification xmlns:wp=\"WPNotification\">" +
            "<wp:Toast>" +
            "<wp:Text1>{0}</wp:Text1>" +
            "<wp:Text2>{1}</wp:Text2>" +
            "</wp:Toast>" +
            "</wp:Notification>", txtMessage, subMessage);
        }

        /// <summary>
        /// XML Tile
        /// </summary>
        /// <param name="image"></param>
        /// <param name="count"></param>
        /// <param name="textMessage"></param>
        /// <returns></returns>
        private static string GetTileXml(string image, int count, string textMessage)
        {
            return string.Format("X-WindowsPhone-Target: toast\r\n\r\n" +
            "<?xml version='1.0' encoding='utf-8'?>" +
            "<wp:Notification xmlns:wp='WPNotification'>" +
            "<wp:Tile>" +
            "<wp:BackgroundImage>{0}</wp:BackgroundImage>" +  
            "<wp:Count>{1}</wp:Count>" +  
            "<wp:Title>{2}</wp:Title>" +  
            "</wp:Tile>" +
            "</wp:Notification>", image, count, textMessage);
        }
    }
}
